package com.rm.zeyou.pay.wxpay.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rm.zeyou.dto.PhoneNumberDTO;
import com.rm.zeyou.mapper.ApplicationConfigMapper;
import com.rm.zeyou.mapper.WxpayConfigMapper;
import com.rm.zeyou.pay.wxpay.common.WechatH5GetMsgUrlConstants;
import com.rm.zeyou.pay.wxpay.service.WechatGetMsgService;
import com.rm.zeyou.pay.wxpay.util.WxLoginUtil;
import com.rm.zeyou.pay.wxpay.util.WxPayUtil;
import com.rm.zeyou.pay.wxpay.vo.DecryptWeChartMsgVO;
import com.rm.zeyou.pay.wxpay.vo.UserDetailsH5VO;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.util.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * @Author ZhangKeChen
 * @Date 2022/11/1
 * @Description
 */
@Slf4j
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class WechatH5GetMsgServiceImpl implements WechatGetMsgService {


    @Resource
    private WxPayUtil wxPayUtil;

    //@Resource
    //private WxPayEntity wxPayEntity;
    @Resource
    private WxpayConfigMapper wxpayConfigMapper;
    @Resource
    private ApplicationConfigMapper applicationConfigMapper;

    /**
     * 获取openid
     *
     * @param code 公众号获取openid的code
     * @return
     */
    @Override
    public DecryptWeChartMsgVO decryptWeChatOpenIdMsg(String code) {
        String accountAppid = applicationConfigMapper.selectOneData("account_appid");
        String accountSecret = applicationConfigMapper.selectOneData("account_secret");
        //用code获取openid请求地址
        String getOpenid = String.format(WechatH5GetMsgUrlConstants.GetOpenIdByCode, accountAppid, accountSecret, code);
        JSONObject userInfo = WxLoginUtil.getUserInFor(getOpenid);
        log.info("用户openid信息：{}", userInfo);
        //使用access_token和openId获取用户详情
        String userDetailsUrl = String.format(WechatH5GetMsgUrlConstants.GetUserDetailsByAccessTokenAndOpenid, userInfo.getString("access_token"), userInfo.getString("openid"));
        JSONObject userDetails = WxLoginUtil.getUserInFor(userDetailsUrl);
        log.info("用户userDetails：{}", userDetails);
        DecryptWeChartMsgVO decryptWeChartMsgVo = new DecryptWeChartMsgVO();
        decryptWeChartMsgVo.setOpenId(userInfo.getString("openid"));
        decryptWeChartMsgVo.setUserDetails(JSONObject.parseObject(String.valueOf(userDetails), UserDetailsH5VO.class));
        decryptWeChartMsgVo.setAccessToken(userInfo.getString("access_token"));
        return decryptWeChartMsgVo;
    }

    /**
     * @param code 公众号获取openid的code
     * @return
     */
    @Override
    public DecryptWeChartMsgVO decryptWeChatPhoneMsg(String code) {
        String accountAppid = applicationConfigMapper.selectOneData("account_appid");
        String accountSecret = applicationConfigMapper.selectOneData("account_secret");
        /**
         * 用appid和secret获取access_token请求地址
         */
        String getTokenUrl = String.format(WechatH5GetMsgUrlConstants.GetAccessTokenByAppIdAndSecret,accountAppid, accountSecret);
        JSONObject userInfo = WxLoginUtil.getUserInFor(getTokenUrl);
        String accessToken = userInfo.getString("access_token");
        /**
         * 使用access_token获取手机号码
         */
        String getPhoneUrl = String.format(WechatH5GetMsgUrlConstants.GetPhoneByAccessToken, accessToken);
        PhoneNumberDTO Phone = new PhoneNumberDTO();
        Phone.setCode(code);
        String s = JSONArray.toJSONString(Phone);
        String phone = HttpUtil.jsonPost(getPhoneUrl, s);
        PhoneNumberDTO phoneNumber = JSONArray.parseObject(phone, PhoneNumberDTO.class);
        log.info("微信授权获取的手机是" + phoneNumber);
        String resultPhone = phoneNumber.getPhone_info().getPhoneNumber();
        log.info("手机号码:" + resultPhone);
        DecryptWeChartMsgVO decryptWeChartMsgVo = new DecryptWeChartMsgVO();
        decryptWeChartMsgVo.setTel(resultPhone);
        return decryptWeChartMsgVo;
    }

    /**
     * 支付config校验接口
     */
    @Override
    public ApiRestResponse payConfig(@RequestParam String url) {
        String accountAppid = applicationConfigMapper.selectOneData("account_appid");
        String accountSecret = applicationConfigMapper.selectOneData("account_secret");
        String getToken = String.format(WechatH5GetMsgUrlConstants.GetAccessTokenByAppIdAndSecret, accountAppid, accountSecret);


        JSONObject userInfo = WxLoginUtil.getUserInFor(getToken);
        log.info("用户openid信息：{}", userInfo);
        log.info("用户access_token：{}", userInfo.getString("access_token"));
        String access_token = userInfo.getString("access_token");
        String jsapiTicket = wxPayUtil.getJsapiTicket(access_token);
        return ApiRestResponse.success(wxPayUtil.sign(jsapiTicket, url));
    }
}
